package poradausersdownloader;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;


/**
 * @author Martin Mihalovic
 */
public class PoradaUsersDownloader {

    private static String targetDir;
    private final String baseUrl = "http://www.porada.sk/member.php?u=";
    final int startUserId = 1;
    final int highestUserId = 248650;       //Highest user ID to the date of downloading discussion data
    
    /**
     * @param args existing folder to which the user profile pages will be downloaded
     */
    public static void main(String[] args) throws Exception {
        
        //Process input argument
        if(args.length == 1) {
            targetDir = args[0];
            if(!targetDir.endsWith("\\"))
                targetDir += "\\";
            File dir = new File(targetDir);
            if(!dir.exists() || !dir.isDirectory()) {
                System.err.println("Cannot find directory " + targetDir);
                System.exit(2);
            }                
        }
        else {
            System.err.println("Usage: program <target_folder>");
            System.exit(1);
        }        
        
        //Do the downloading
        PoradaUsersDownloader downloader = new PoradaUsersDownloader();
        downloader.download();
        System.out.println("\nFINISHED (Enter to quit)");
        System.in.read();
    }
    
    
    private void download() throws Exception {
        log("================== User profiles downloading started at "+new Date()+" ==================\n\n");
        
        for(int i=startUserId; i <= highestUserId; i++)
        {
            URL link = new URL(baseUrl + i);

            try{
                URLConnection conn = link.openConnection();

                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                save(i, in);
                in.close();
                
                if (i%100 == 0)
                    System.out.println("Downloaded profile from " + link.toString());
            }
            catch(Exception ex) {
                System.err.println("Error getting content from " + link.toString() + "\n(" + ex.getMessage() +") See log file for more info");
                log(ex.getStackTrace().toString());
            }
        }
    }
    
    
    private void save(int id, BufferedReader pageReader) throws IOException {
        FileWriter writer = new FileWriter(targetDir + id + ".html");
        BufferedWriter out = new BufferedWriter(writer);
        
        String line;
        while((line = pageReader.readLine()) != null) {
            out.write(line);
            out.newLine();
        }
        out.close();
    }
    
    
    private void log(String message) throws IOException {
        FileWriter fstream = new FileWriter("UsersDownload.log", true);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(message);

        out.close();
    }
}
